package com.zyf.api;

import com.zyf.common.result.Result;
import com.zyf.common.util.AuthContextHolder;
import com.zyf.model.user.Patient;
import com.zyf.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/** 就诊人管理的Controller
 * @author zyf
 * @date 2021年05月28日 11:28
 */
@Api("就诊人管理")
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;


    @ApiOperation(value = "根据用户id获取就诊人列表")
    @GetMapping("/auth/findAll")
    public Result findAllPatient(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList= patientService.findAllPatientByUserId(userId);
        return Result.ok(patientList);
    }


    @ApiOperation(value = "添加就诊人")
    @PostMapping("/auth/savePatient")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }


    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("/auth/findPatientById/{id}")
    public Result  findPatientById(@PathVariable long id){
        Patient patient = patientService.findPatientById(id);
        return Result.ok(patient);
    }

    @ApiOperation(value = "修改就诊人信息")
    @PutMapping("/auth/updatePatientById")
    public Result  updatePatientById(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }


    @ApiOperation(value = "删除就诊人信息")
    @DeleteMapping("/auth/removePatientById/{id}")
    public Result  removePatientById(@PathVariable long id){
        patientService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("/inner/get/{id}")
    public Patient getPatientOrder(@PathVariable long id){
        Patient patient = patientService.findPatientById(id);
        return patient;
    }
}
